//
//  RegisterViewController.m
//  myhuibao
//
//  Created by lijun on 13-4-16.
//  Copyright (c) 2013年 myhuibao. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "RegisterViewController.h"
#import "SendIdcodeViewController.h"
#import<QuartzCore/QuartzCore.h>
#import "Config.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJson.h"
#import "RegAgreementViewController.h"
#import "OMGToast.h"
@interface RegisterViewController ()

@end

@implementation RegisterViewController
@synthesize phone;
@synthesize mainview;
@synthesize exphone;
@synthesize nextBtn;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    exphone.enabled=NO;
    mainview.layer.cornerRadius=5;
    mainview.layer.borderWidth=1;
    mainview.layer.borderColor=[UIColor lightGrayColor].CGColor;
    [[mainview layer] setShadowOffset:CGSizeMake(1, 1)];
    [[mainview layer] setShadowRadius:1];
    [[mainview layer] setShadowOpacity:1];
    [[mainview layer] setShadowColor:[UIColor lightGrayColor].CGColor];
    phone.delegate = self;
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)hasNext:(id)sender {
    if([phone.text isEqualToString:@"" ]||[phone.text  isEqual:nil]){
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:APP_TITLE message:@"您还没有输入手机号码哦!"delegate:nil
            cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if([Config isMobileNumber:phone.text]==NO ){
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:APP_TITLE message:@"请输入11位的手机号码"delegate:nil
                                           cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    NSURL *url=[NSURL URLWithString:url_reg];
    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    [request setPostValue:phone.text forKey:@"member.mobile"];
    [request setDelegate:self];
    [request startSynchronous];
    NSString *jsonStr = [request responseString];
    NSDictionary *dic = [jsonStr JSONValue];
    if([[dic valueForKey:@"status"] isEqualToString:@"ERROR"]){
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:APP_TITLE message:
                            [dic valueForKey:@"log"]
                                                    delegate:nil
                                           cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        phone.text=nil;
        return;
    }

    if ([[dic valueForKey:@"status"] isEqualToString:@"SUCCESS"]) {
         SendIdcodeViewController *view=[[SendIdcodeViewController alloc]initWithNibName:@"SendIdcodeViewController" bundle:nil];
//        view.phone=phone.text;
        [self.navigationController pushViewController:view animated:YES];
    }
//    else{
//        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:APP_TITLE message:
//                            [dic valueForKey:@"log"]
//                                                    delegate:nil
//                                           cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alert show];
//        [alert release];
//        return;
//    }
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@",error);
    if (error.code==net_failure_code) {
        [OMGToast showWithText:net_failure_msg bottomOffset:100];
    }
}

- (IBAction)goBakc:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)showAgment:(id)sender {
    RegAgreementViewController *view=[[RegAgreementViewController alloc]initWithNibName:@"RegAgreementViewController" bundle:nil];
    [self.navigationController pushViewController:view animated:YES];
}

- (IBAction)editphone:(id)sender {
    phone.keyboardType= UIKeyboardTypeNumbersAndPunctuation;
//    float height = 215.0;
//    CGRect frame = self.view.frame;
//    frame.origin.y=(frame.size.height-self.nextBtn.frame.size.height-self.nextBtn.frame.origin.y)-height;
//    [UIView beginAnimations:@"Curl"context:nil];//动画开始
//    [UIView setAnimationDuration:0.30];
//    [UIView setAnimationDelegate:self];
//    [self.view setFrame:frame];
//    [UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    //    if (theTextField == self.textField) {
    [theTextField resignFirstResponder];
    //    }
    return YES;
}

@end
